<?php
if ($_POST['submit']!='')
{
	$cat=$_POST['cat'];
	if ($_GET['edit']) 
	$cmd='UPDATE `cat` SET cat_name="'.$_POST['cat'].'" WHERE cat_id='.$_GET['edit'];
	else
	$cmd="INSERT INTO `cat` (`cat_name`) VALUES ('$cat')";
	mysql_query($cmd);
	echo 'Đã hoàn tất !';	
} else
{
if ((isset($_GET['act']))||(isset($_GET['edit'])))
{
	if ($_GET['edit']){
		$cmd="SELECT * FROM `cat` WHERE cat_id=".$_GET['edit'];	
		$info=mysql_fetch_assoc(mysql_query($cmd));
	}
	echo '<form method="post"> 
	<div class="selecttitle"><a><b>';
	if ($_GET['edit']) echo 'SỬA '; else echo 'THÊM ';
	echo 'THỂ LOẠI </b></a></div><br/>
	<div class="selecttitle"><a>Tên thể loại</a></div></td>
    <div class="selecttitle"><a><input name="cat" type="text" ';
	if ($_GET['edit']) echo 'value="'.$info['cat_name'].'"';
	echo '/></a></div>
    <br/>
	<input type="submit" class="buttonclick" value="Lưu" name="submit"/>
	</form>
	';	
}
else
{
echo'<div id="space"><div class="buttons"><a href="/mem/?menu=category&act=add" class="button add">Thêm</a></div></div>
<div id="view">
<table width="775" border="1" class="view" cellpadding="10" cellspacing="0">
  <th width="15">
  <form id="form1" name="form1" method="post" action="">
    <input type="checkbox" name="checkbox" id="checkbox" />
  </form> 
  </th>	
  <th width="30px">ID</th>
  <th>Thể loại</th>
  <th width="180px">Số bài hát</th>
  <th width="40px">Thứ tự</th>
  <th width="40px">Xóa</th>';
  	  $sql="SELECT * FROM `cat` ORDER BY  `cat_id` ASC LIMIT 0,20";
	  $excute=mysql_query($sql);
	  $order=0;
	  while ($row=mysql_fetch_array($excute,MYSQL_ASSOC))
	  {
		  $order++;
		  echo'<tr><td><form id="form1" name="form1" method="post" action="">
    	  <input type="checkbox" name="checkbox" id="checkbox" /></form> </td><td>';
		  echo $row['cat_id']; echo '</td><td>';
		  echo $row['cat_name']; echo '</td><td>';
		  $dem=$row['cat_id'];
		  $sql="SELECT `media_id` FROM `media` WHERE media_cat=$dem";
	  	  echo mysql_num_rows(mysql_query($sql)).'</td>';
		  echo '<td>'.$order.'</td>';
		  echo '<td><div class="buttons"><a class="button flag" href="?menu=category&edit='.$row['cat_id'].'">Sửa</a></div></td></tr>';
	  }
echo '</table>
</div>';
}}?>

